package com.cysion.otbox.ui;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cysion.otbox.R;


/**
 * Created by xianshang.liu on 2017/5/17.
 *
 * //        leftShow 0-不显示，1显示文本，2显示图标（默认）
 * //        rightShow 0-不显示，1显示文本（默认），2显示图标
 * //        showTitle 标题展示，0不显示，1 展示
 *           leftOption   默认左按钮操作，0默认操作，1 自定义操作 通过 topClickLisener
 *
 */

public class MyCyTopBar extends RelativeLayout {

    private View mRootView;
    private TextView mTxtTitle;
    private ImageView mImgRight;
    private ImageView mImgLeft;
    private TextView mTvRight;
    private TextView mTvLeft;
    private OnTopBarClickListener mOnTopBarClickListener = new OnTopBarClickListener() {
        @Override
        public void onIconClicked(View aView, Pos aPosition) {
            try {
                throw new Exception("should invoke set method");
            } catch (Exception aE) {
                aE.printStackTrace();
            }
        }
    };
    private OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int viewId = v.getId();
            if (viewId == R.id.ivLeft) {
                if(leftOption==0){
                    if (getContext() instanceof Activity) {
                        ((Activity) getContext()).finish();
                    }
                }else {
                    mOnTopBarClickListener.onIconClicked(v, Pos.LEFT);
                }
            } else if (viewId == R.id.tvLeft) {
                mOnTopBarClickListener.onIconClicked(v, Pos.LEFT);
            } else if (viewId == R.id.tvTopbarTitle) {
                mOnTopBarClickListener.onIconClicked(v, Pos.MIDDLE);
            } else if (viewId == R.id.ivRight) {
                mOnTopBarClickListener.onIconClicked(v, Pos.RIGHT);
            } else if (viewId == R.id.tvRight) {
                mOnTopBarClickListener.onIconClicked(v, Pos.RIGHT);
            }
        }
    };
    private int rightShow;
    private int leftOption;

    public enum Pos {
        LEFT, RIGHT, MIDDLE
    }

    public interface OnTopBarClickListener {
        void onIconClicked(View aView, Pos aPosition);
    }

    public MyCyTopBar(Context context) {
        super(context);
    }

    public MyCyTopBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyCyTopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context aContext, AttributeSet attrs) {
//        leftShow 0-不显示，1显示文本，2显示图标（默认）  3  取消自动返回
//        rightShow 0-不显示，1显示文本（默认），2显示图标
//        showTitle 标题展示，0不显示，1 展示
        TypedArray ta = aContext.obtainStyledAttributes(attrs, R.styleable.MyCyTopBar);
        int leftShow = ta.getInt(R.styleable.MyCyTopBar_cy_topbar_leftShow, 2);
        int titleShow = ta.getInt(R.styleable.MyCyTopBar_cy_topbar_showTitle, 1);
        rightShow = ta.getInt(R.styleable.MyCyTopBar_cy_topbar_rightShow, 1);
        leftOption = ta.getInt(R.styleable.MyCyTopBar_cy_topbar_leftOption,0 );
        int leftRes = ta.getResourceId(R.styleable.MyCyTopBar_cy_topbar_leftImageRes, 0);
        int rightRes = ta.getResourceId(R.styleable.MyCyTopBar_cy_topbar_rightImageRes, 0);
        String title = ta.getString(R.styleable.MyCyTopBar_cy_topbar_titleText);
        String leftText = ta.getString(R.styleable.MyCyTopBar_cy_topbar_leftText);
        String rightText = ta.getString(R.styleable.MyCyTopBar_cy_topbar_rightText);
        ta.recycle();


        LayoutInflater inflater = (LayoutInflater) aContext.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        mRootView = inflater.inflate(R.layout.base_ui_top_bar, this);
        mImgLeft = (ImageView) mRootView.findViewById(R.id.ivLeft);
        mImgRight = (ImageView) mRootView.findViewById(R.id.ivRight);
        mTxtTitle = (TextView) mRootView.findViewById(R.id.tvTopbarTitle);
        mTvRight = (TextView) mRootView.findViewById(R.id.tvRight);
        mTvLeft = (TextView) mRootView.findViewById(R.id.tvLeft);

        if(leftShow==0){
            mTvLeft.setVisibility(View.GONE);
            mImgLeft.setVisibility(View.GONE);
        }else if(leftShow==1){
            mTvLeft.setVisibility(View.VISIBLE);
            mImgLeft.setVisibility(View.GONE);
        }else{
            mTvLeft.setVisibility(View.GONE);
            mImgLeft.setVisibility(View.VISIBLE);
        }

        if(rightShow ==0){
            mTvRight.setVisibility(View.GONE);
            mImgRight.setVisibility(View.GONE);
        }else if(rightShow ==1){
            mTvRight.setVisibility(View.VISIBLE);
            mImgRight.setVisibility(View.GONE);
        }else{
            mTvRight.setVisibility(View.GONE);
            mImgRight.setVisibility(View.VISIBLE);
        }

        if(titleShow==0){
            mTxtTitle.setVisibility(View.GONE);
        }else{
            mTxtTitle.setVisibility(View.VISIBLE);
        }

        if(leftRes!=0&&leftRes!=-1){
            mImgLeft.setImageResource(leftRes);
        }
        if(rightRes!=0&&rightRes!=-1){
            mImgRight.setImageResource(rightRes);
        }
        mTvLeft.setText(leftText);
        mTvRight.setText(rightText);
        mTxtTitle.setText(title);

        mImgLeft.setOnClickListener(mOnClickListener);
        mTvLeft.setOnClickListener(mOnClickListener);
        mTxtTitle.setOnClickListener(mOnClickListener);
        mImgRight.setOnClickListener(mOnClickListener);
        mTvRight.setOnClickListener(mOnClickListener);
    }

    public void setOnTopBarClickListener(OnTopBarClickListener aOnTopBarClickListener) {
        mOnTopBarClickListener = aOnTopBarClickListener;
    }

    public void setImageRes(int resId, Pos aPos) {
        if (aPos == Pos.LEFT) {
            mImgLeft.setImageResource(resId);
        } else if (aPos == Pos.RIGHT) {
            mImgRight.setImageResource(resId);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    //设置标题
    public void setTitle(String text) {
        mTxtTitle.setText(text);
    }

    //设置不同位置的文本
    public void setTextPos(String text, Pos aPos){
        if (aPos == Pos.LEFT) {
            mTvLeft.setText(text);
        } else if (aPos == Pos.RIGHT) {
            mTvRight.setText(text);
        }else{
            mTxtTitle.setText(text);
        }
    }

    //显示右边图标
    public void showImgRight() {
        mTvRight.setVisibility(GONE);
        mImgRight.setVisibility(VISIBLE);
    }

    //显示右边文本
    public void showTxtRight(String text) {
        mTvRight.setVisibility(VISIBLE);
        mImgRight.setVisibility(GONE);
        mTvRight.setText(text);
    }


    //设置左边图标
    public void showImgLeft() {
        mTvLeft.setVisibility(GONE);
        mImgLeft.setVisibility(VISIBLE);
    }


    // 控制右边的按钮显示隐藏
    public void setRightVisible(boolean isShow){
        if(isShow){
            if(rightShow==1){
                mTvRight.setVisibility(View.VISIBLE);
                mImgRight.setVisibility(View.GONE);
            }else if(rightShow==2){
                mTvRight.setVisibility(View.GONE);
                mImgRight.setVisibility(View.VISIBLE);
            }
        }else {
            mTvRight.setVisibility(View.GONE);
            mImgRight.setVisibility(View.GONE);
        }
    }


    public ImageView getRightImageView(){
        return mImgRight;
    }

    //显示左边文本
    public void showTxtLeft(String text) {
        mTvLeft.setVisibility(VISIBLE);
        mImgLeft.setVisibility(GONE);
        mTvLeft.setText(text);
    }
}
